package com.example.music.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.lib_leancloud.model.ChartMusicModel
import com.example.music.databinding.ChartItemBinding

class chartAdapter : RecyclerView.Adapter<chartAdapter.MyViewHolder>() {
    private var chartMusicList = listOf<ChartMusicModel>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)
            : MyViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return MyViewHolder(ChartItemBinding.inflate(inflater))
    }

    override fun getItemCount(): Int {
        return chartMusicList.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.binding(chartMusicList[position])
    }

    class MyViewHolder(val binding: ChartItemBinding) : RecyclerView.ViewHolder(binding.root) {

        fun binding(model: ChartMusicModel) {
            binding.chartTitle.text = model.title
            binding.checkRecycleView.apply {
                adapter = MusicAdapter().apply {
                    setNewData(model.music)
                }
                layoutManager = GridLayoutManager(
                    context, 2, GridLayoutManager.HORIZONTAL, false
                )


            }
        }
    }

    fun setNewData(newData: List<ChartMusicModel>) {
        chartMusicList = newData
        notifyDataSetChanged()
    }
}